﻿//-----------------------------------------------------------------------
// <copyright file="CheckHelper.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: CheckHelper.cs
// * history : created by qinchaoyue 2018-01-10 01:39:47
// </copyright>
//-----------------------------------------------------------------------

using System;
using Slwy.Utility.SlwyException;

namespace Slwy.Utility.Helper
{
    public static class CheckHelper
    {
        /// <summary>
        /// 检查表达式是否返回 True
        /// 如果表达式返回 True 则结束程序, 返回传入的 busCode 和 msg 给调用方
        /// </summary>
        /// <param name="isSuccess">表达式</param>
        /// <param name="returnModel">返回的结果</param>
        public static void ThrowExpressIsTrue(Func<bool> isSuccess, object returnModel)
        {
            if (isSuccess.Invoke())
            {
                throw new SlwyArgumentNullException(returnModel);
            }
        }

        /// <summary>
        /// 检查表达式是否返回 False
        /// 如果表达式返回 false 则结束程序, 返回传入的 busCode 和 msg 给调用方
        /// </summary>
        /// <param name="isSuccess">表达式</param>
        /// <param name="returnModel">返回的结果</param>
        public static void ThrowExpressIsFalse(Func<bool> isSuccess, object returnModel)
        {
            if (!isSuccess.Invoke())
            {
                throw new SlwyArgumentNullException(returnModel);
            }
        }

        /// <summary>
        /// 检查表达式是否返回 True
        /// 如果表达式返回 True 则结束程序, 返回传入的 busCode 和 msg 给调用方
        /// </summary>
        /// <param name="isSuccess">表达式</param>
        /// <param name="busCode">busCode</param>
        /// <param name="msg">消息</param>
        public static void ThrowExpressIsTrue(Func<bool> isSuccess, int busCode = 0, string msg = "")
        {

            ThrowExpressIsTrue(isSuccess, new { BusCode = busCode, Msg = msg });
        }

        /// <summary>
        /// 检查表达式是否返回 False
        /// 如果表达式返回 false 则结束程序, 返回传入的 busCode 和 msg 给调用方
        /// </summary>
        /// <param name="isSuccess">表达式</param>
        /// <param name="busCode">busCode</param>
        /// <param name="msg">消息</param>
        public static void ThrowExpressIsFalse(Func<bool> isSuccess, int busCode = 0, string msg = "")
        {
            ThrowExpressIsFalse(isSuccess, new { BusCode = busCode, Msg = msg });
        }
    }
}
